package com.zhuhjay.chat.server.handler;

import com.zhuhjay.chat.message.GroupMembersRequestMessage;
import com.zhuhjay.chat.message.GroupMembersResponseMessage;
import com.zhuhjay.chat.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;

import java.util.Set;

/**
 * @author ZhuHJay
 * @date 2023/3/8 22:17
 */
@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends AbstractRequestMessageHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        if (GroupSessionFactory.getGroupSession().isGroupExist(msg.getGroupName())) {
            ctx.writeAndFlush(new GroupMembersResponseMessage(false, "群聊[" + msg.getGroupName() + "]不存在"));
            return;
        }
        Set<String> members = GroupSessionFactory.getGroupSession().getMembers(msg.getGroupName());
        ctx.writeAndFlush(new GroupMembersResponseMessage(members));
    }
}
